package sgi.client;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.imageio.ImageIO;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class SGIImageAddUI extends SGIClientFrame {
	SGIImageAddController ImageAddController;
	private JTextField txtDate, txtLocation, txtLatitude, txtLongitude,txtSample;
	JFileChooser fileChooser;
	private JPanel Panel;
	private BufferedImage originalImage1;
	private BufferedImage originalImage2;
	private BufferedImage originalImage3;
	private BufferedImage originalImage4;
	private BufferedImage originalImage5;
	private JTextField txtData1;
	private JTextField txtData2;
	private JTextField txtData3;
	private JTextField txtData4;
	private byte[] photo1;
	private byte[] photo2;
	private byte[] photo3;
	private byte[] photo4;
	private byte[] photo5;
	JDatePickerImpl datePickerTo;
	SpringLayout springLayout;
	private SpringLayout sl_datePickerTo;
	public SGIImageAddUI()
	{
		/** labels and texts that are required for image add */
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 465, 409);
		Panel= new JPanel();
		setTitle("Image Uploader");
		setVisible(true);
		setResizable(false);
		Panel.setLayout(null);
		setContentPane(Panel);
		/** getting date */
		UtilDateModel model = new UtilDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(model);
		datePickerTo = new JDatePickerImpl(datePanel);
		springLayout = new SpringLayout();
		springLayout.putConstraint(SpringLayout.NORTH, datePickerTo.getJFormattedTextField(), 0, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.WEST, datePickerTo.getJFormattedTextField(), -90, SpringLayout.EAST, datePickerTo);
		springLayout.putConstraint(SpringLayout.SOUTH, datePickerTo.getJFormattedTextField(), 23, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.EAST, datePickerTo.getJFormattedTextField(), -35, SpringLayout.EAST, datePickerTo);
		sl_datePickerTo = (SpringLayout) datePickerTo.getLayout();
		datePickerTo.setBounds(63, 15, 145, 26);
		Panel.add(datePickerTo);
		
		JLabel lbldate = new JLabel("Date:");
		lbldate.setBounds(10, 15, 72, 26);
		Panel.add(lbldate);
			
		JLabel lblLocation = new JLabel("Location:");
		lblLocation.setBounds(218, 15, 72, 26);
		Panel.add(lblLocation);
		
		txtLocation = new JTextField();
		txtLocation.setBounds(286, 15, 154, 26);
		txtLocation.setColumns(10);
		Panel.add(txtLocation);
		
		JLabel lblLatitude = new JLabel("Latitude:");
		lblLatitude.setBounds(10, 52, 72, 26);
		Panel.add(lblLatitude);
		
		txtLatitude = new JTextField();
		txtLatitude.setBounds(63, 52, 145, 26);
		txtLatitude.setColumns(10);
		Panel.add(txtLatitude);
		
		JLabel lblLongitude = new JLabel("Longitude:");
		lblLongitude.setBounds(218, 52, 72, 26);
		Panel.add(lblLongitude);
		
		txtLongitude = new JTextField();
		txtLongitude.setBounds(286, 52, 154, 26);
		txtLongitude.setColumns(10);
		Panel.add(txtLongitude);
		
		JLabel lblChooseSample = new JLabel("Choose Image Sample:");
		lblChooseSample.setBounds(10, 114, 148, 34);
		Panel.add(lblChooseSample);
		
		txtSample = new JTextField();
		txtSample.setBounds(160, 115, 158, 32);
		Panel.add(txtSample);
		txtSample.setColumns(10);
		
		JLabel lblChooseData1 = new JLabel("Choose Image data Layer 1:");
		lblChooseData1.setBounds(10, 159, 148, 34);
		Panel.add(lblChooseData1);
		
		txtData1 = new JTextField();
		txtData1.setColumns(10);
		txtData1.setBounds(160, 160, 158, 32);
		Panel.add(txtData1);
		
		JLabel lblChooseData2 = new JLabel("Choose Image data Layer 2:");
		lblChooseData2.setBounds(10, 204, 148, 34);
		Panel.add(lblChooseData2);
		
		txtData2 = new JTextField();
		txtData2.setColumns(10);
		txtData2.setBounds(160, 205, 158, 32);
		Panel.add(txtData2);
		
		JLabel lblChooseData3 = new JLabel("Choose Image data Layer 3:");
		lblChooseData3.setBounds(10, 249, 148, 34);
		Panel.add(lblChooseData3);
		
		txtData3 = new JTextField();
		txtData3.setColumns(10);
		txtData3.setBounds(160, 250, 158, 32);
		Panel.add(txtData3);
		
		JLabel lblChooseData4 = new JLabel("Choose Image data Layer 4:");
		lblChooseData4.setBounds(10, 299, 148, 34);
		Panel.add(lblChooseData4);

		txtData4 = new JTextField();
		txtData4.setColumns(10);
		txtData4.setBounds(160, 300, 158, 32);
		Panel.add(txtData4);
		/** button of adding sample image */
		JButton btnBrowseSample = new JButton("Browse:");
		btnBrowseSample.setBounds(351, 114, 89, 34);
		btnBrowseSample.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					
				txtSample.setText(chooseImage(0));
		}});
		Panel.add(btnBrowseSample);
		/** button of adding datalayer1 image */
		JButton btnBrowseData1 = new JButton("Browse:");
		btnBrowseData1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData1.setText(chooseImage(1));
		}});
		btnBrowseData1.setBounds(351, 159, 89, 34);
		Panel.add(btnBrowseData1);
		/** button of adding datalayer2 image */
		JButton btnBrowseData2 = new JButton("Browse:");
		btnBrowseData2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData2.setText(chooseImage(2));
		}});
		btnBrowseData2.setBounds(351, 204, 89, 34);
		Panel.add(btnBrowseData2);
		/** button of adding datalayer3 image */
		JButton btnBrowseData3 = new JButton("Browse:");
		btnBrowseData3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData3.setText(chooseImage(3));
		}});
		btnBrowseData3.setBounds(351, 249, 89, 34);
		Panel.add(btnBrowseData3);
		/** button of adding datalayer4 image */
		JButton btnBrowseData4 = new JButton("Browse:");
		btnBrowseData4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData4.setText(chooseImage(4));
		}});
		btnBrowseData4.setBounds(351, 299, 89, 34);
		Panel.add(btnBrowseData4);
		/** button of uploading all fields to insert to images database table */
		JButton btnUpload = new JButton("Upload");
		btnUpload.setBounds(317, 343, 123, 26);
		btnUpload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Date selectedDateTo = (Date)datePickerTo.getModel().getValue();
				if(selectedDateTo == null || txtLocation.getText().equals("")
					|| txtLatitude.getText().equals("") || txtLongitude.getText().equals("") || txtSample.getText().equals("")
					|| txtData1.getText().equals("") || txtData2.getText().equals("") 
					|| txtData3.getText().equals("")|| txtData4.getText().equals(""))
				{
					ImageAddController.FieldNotInserted();
					SGIImageAddController.terminate();
					try {
						SGIImageAddController.create();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else
				{
					ImageAddController.update((selectedDateTo.getYear()+1900)+"-"+(selectedDateTo.getMonth()+1)+"-"+(selectedDateTo.getDate())
					,txtLocation.getText(), txtLatitude.getText(), txtLongitude.getText(),
					photo1, photo2, photo3, photo4, photo5 );
				}
			}
		});
		Panel.add(btnUpload);
		/** button of returning to user account menu */
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGIImageAddController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		btnCancel.setBounds(10, 344, 123, 24);
		Panel.add(btnCancel);
		

	}
	/** get controller  */
	public SGIImageAddController getAccountInfoController() {
		return ImageAddController;
	}
	
	/** set controller */
	public void setSGIImageAddController(SGIImageAddController ImageAddController) {
		this.ImageAddController = ImageAddController;
	}
	/** method that defines which "Browse" button we pressed and then adding to the correct place  */
	public String chooseImage(int button1)
	{
		JFileChooser chooser = new JFileChooser();			    
		chooser.setDialogTitle("Choose Your File");
        FileNameExtensionFilter  filterJPEG = new FileNameExtensionFilter("JPEG", "jpeg");
        FileNameExtensionFilter  filterJPG = new FileNameExtensionFilter("JPG", "jpg");
        chooser.addChoosableFileFilter(filterJPG);
        chooser.addChoosableFileFilter(filterJPEG);
        
        chooser.setFileFilter(filterJPG);
        chooser.setAcceptAllFileFilterUsed(false);
        int returnval=chooser.showOpenDialog(SGIImageAddController.getUI());
        if(returnval==JFileChooser.APPROVE_OPTION)
        {
        	File image = chooser.getSelectedFile();
        	switch(button1)
        	{
        		case 0:
        		{
        			try
        			{
        				originalImage1 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage1, "jpg", baos);
        				photo1 = baos.toByteArray();
        				
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 1:
        		{
        			try
        			{
        				originalImage2 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage2, "jpg", baos);
        				photo2 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 2:
        		{
        			try
        			{
        				originalImage3 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage3, "jpg", baos);
        				photo3 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 3:
        		{
        			try
        			{
        				originalImage4 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage4, "jpg", baos);
        				photo4 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 4:
        		{
        			try
        			{
        				originalImage5 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage5, "jpg", baos);
        				photo5 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        	}
        	return chooser.getSelectedFile().getAbsolutePath();
        }
        else
        	return "";
 
	}
	/** notifying that images have been uplaoded */
	public void notify(String str)
	{
		JOptionPane.showMessageDialog(this,str);
	}
}
